package cn.iocoder.yudao.module.pay.api.shipping;

import cn.hutool.http.HttpUtil;
import cn.iocoder.yudao.module.pay.controller.admin.channel.vo.PayChannelRespVO;
import cn.iocoder.yudao.module.pay.api.shipping.dto.WxDeliverGoodsDTO;
import cn.iocoder.yudao.module.pay.convert.channel.PayChannelConvert;
import cn.iocoder.yudao.module.pay.dal.dataobject.channel.PayChannelDO;
import cn.iocoder.yudao.module.pay.service.channel.PayChannelService;
import cn.iocoder.yudao.module.system.api.wechat_card.WeChatCardApi;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


/**
 * 微信小程序 发货
 */
@Slf4j
@Service
public class WeChatApiImpl implements WeChatApi {

    @Resource
    private PayChannelService channelService;

    @Resource
    private WeChatCardApi weChatCardApi;



    //参考文档 https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order-shipping/order-shipping.html
    private static final String uploadShippingInfo = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info";


    /**
     * 微信小程序-发货信息录入接口
     * @param vo
     */
    public Boolean uploadShippingInfo(WxDeliverGoodsDTO vo) {
        /*WxDeliverGoodsDTO vo = new WxDeliverGoodsDTO();
        OrderKeyDTO order_key = new OrderKeyDTO();
        order_key.setMchid("");
        order_key.setOrder_number_type(2);    // 必填
        order_key.setOut_trade_no("");
        order_key.setTransaction_id("fake-transid-20221214190427-1");

        ArrayList<ShippingDTO> list = new ArrayList<>();
        ShippingDTO shipping = new ShippingDTO();
        shipping.setExpress_company("STO");
        shipping.setItem_desc("微信气泡狗集线器*1");    // 必填
        shipping.setTracking_no("fake-trackingno-2022121419042711");
        ContactDTO contact = new ContactDTO();
//		contact.setConsignor_contact("+86-177****1234");
        contact.setConsignor_contact("13278653987".replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));    // 手机号掩码处理
        contact.setReceiver_contact("");
        shipping.setContact(contact);
        list.add(shipping);

        PayerDTO payer = new PayerDTO();
        payer.setOpenid("ogqztkPsejM9MQAFfwCQSCi4oNg3");    // 必填

        vo.setOrder_key(order_key);    // 必填
        vo.setLogistics_type(1);    // 必填
        vo.setDelivery_mode(1);        // 必填
        vo.setIs_all_delivered(false);    // 分拆发货模式时必填
        vo.setShipping_list(list);    // 必填
//		vo.setUpload_time("2022-12-15T13:29:35.120+08:00");	// 必填
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        vo.setUpload_time(sdf.format(new Date()));    // 必填
        vo.setPayer(payer);    // 必填*/
        PayChannelDO wxLiteChannel = channelService.getChannelByAppIdAndCode(1l, "wx_lite");
        if (null != wxLiteChannel) {
            PayChannelRespVO wxLiteConfig = PayChannelConvert.INSTANCE.convert(wxLiteChannel);
            JSONObject config = JSONObject.parseObject(wxLiteConfig.getConfig());
            vo.getOrder_key().setMchid(config.getString("mchId"));
        }
        String accessToken = weChatCardApi.getAccessToken();
        String url = uploadShippingInfo + "?access_token=" + accessToken;
        /* 获取模拟入参 */
        String param = JSONObject.toJSONString(vo);
        System.out.println("入参 = " + param);
        String result = HttpUtil.post(url, param);

        JSONObject jsonObject = JSON.parseObject(result);
        int errcode = jsonObject.getInteger("errcode");
        if (errcode != 0) {
            String errmsg = jsonObject.getString("errmsg");
            String err = String.format("微信小程序-发货信息录入接口异常，code码：%s, msg：%s", errcode, errmsg);
            log.error(err);
            return false;
        }
        return true;
    }


}



